#pragma GCC optimize(2)
#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
#include <bitset>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int N=2e5+10;
ll a[N];
ll b[N];
int main()
{
	IOS;
	int t;
	cin>>t;
	while(t--)
	{
		map<ll,ll> mp;
		int n;
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			mp[a[i]]++;
		}
		sort(a+1,a+1+n);
		int idx=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]!=a[i-1])
			{
				b[++idx]=a[i];
			}
		}
		int q;
		cin>>q;
		int r=sqrt(idx);
//		cout<<r<<endl;
		while(q--)
		{
			ll x,y;
			cin>>x>>y;
			ll sum=0;
			for(int i=1;i<=r;i++)
			{
				if(y%b[i]==0)
				{
			        ll c=y/b[i];
//			        cout<<c<<endl;
			        if(mp[c]==0) continue;
			        if(c==b[i])
			        {
			        	sum+=mp[c]*(mp[c]-1)*2;
					}
					else sum+=mp[b[i]]*mp[c];
				}
			}
			cout<<sum<<' ';
			
		}
		cout<<'\n';
	}

	return 0;
}

